package com.zou.lotterylucky.utils

import android.content.Context
import android.view.View
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.Toast
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog
import io.reactivex.Flowable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import java.util.concurrent.TimeUnit

object ToastUtil {

    private var oldMsg: String? = null
    internal var toast: Toast? = null
    private var oneTime: Long = 0
    private var twoTime: Long = 0

    fun showMsg(context: Context, msg: String) {
        Toast.makeText(context, msg, Toast.LENGTH_SHORT).show()
    }

    fun showMsg(context: Context, resId: Int) {
        Toast.makeText(context, resId, Toast.LENGTH_SHORT).show()
    }

    fun showToast(context: Context, s: String) {
        if (toast == null) {
            toast = Toast.makeText(context, s, Toast.LENGTH_SHORT)
            try {
                val view = toast!!.view as LinearLayout
                view.gravity = View.TEXT_ALIGNMENT_CENTER
            } catch (e: Exception) {
                val view = toast!!.view as RelativeLayout
                view.gravity = RelativeLayout.CENTER_IN_PARENT
            }

            toast!!.show()
            oneTime = System.currentTimeMillis()
        } else {
            twoTime = System.currentTimeMillis()
            if (s == oldMsg) {
                if (twoTime - oneTime > Toast.LENGTH_SHORT) {
                    toast!!.show()
                }
            } else {
                oldMsg = s
                toast!!.setText(s)
                toast!!.show()
            }
        }
        oneTime = twoTime
    }


    fun showToast(context: Context, resId: Int) {
        showToast(context, context.getString(resId))
    }

    fun showTips(context: Context, success: Boolean) {
        val builder = QMUITipDialog.Builder(context)
        val dialog =
            builder.setIconType(if (success) QMUITipDialog.Builder.ICON_TYPE_SUCCESS else QMUITipDialog.Builder.ICON_TYPE_FAIL)
                .setTipWord(if (success) "Success" else "Failed")
                .create()
        dialog.setCancelable(false)
        dialog.show()

        Flowable.timer(800, TimeUnit.MILLISECONDS, Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { dialog.dismiss() }
    }


    fun showTipsNotFound(context: Context) {
        val builder = QMUITipDialog.Builder(context)
        val dialog = builder.setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
            .setTipWord("未找到该订单")
            .create()
        dialog.setCancelable(false)
        dialog.show()

        Flowable.timer(800, TimeUnit.MILLISECONDS, Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { dialog.dismiss() }
    }

    fun showTipsError(context: Context, errorWord: String) {
        val builder = QMUITipDialog.Builder(context)
        val dialog = builder.setIconType(QMUITipDialog.Builder.ICON_TYPE_FAIL)
            .setTipWord(errorWord)
            .create()
        dialog.setCancelable(false)
        dialog.show()

        Flowable.timer(800, TimeUnit.MILLISECONDS, Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe { dialog.dismiss() }
    }

    /*public static void showMoneyLimit(@NotNull Context context, int max) {
        QMUIDialog.MessageDialogBuilder builder = new QMUIDialog.MessageDialogBuilder(context);
        builder.setTitle(R.string.tishi);
        if (max >= 0) {
            builder.setMessage(context.getString(R.string.bunengcaoguo) + "\n" + context.getString(R.string.this_limit) + max);
        } else {
            builder.setMessage(R.string.bunengcaoguo);
        }


        builder.addAction(new QMUIDialogAction(context, context.getString(R.string.confirm), new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog qmuiDialog, int i) {
                qmuiDialog.dismiss();
            }
        }));

        QMUIDialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
    }

    public static void showMoneyError(@NotNull Context context) {
        QMUIDialog.MessageDialogBuilder builder = new QMUIDialog.MessageDialogBuilder(context);
        builder.setTitle(R.string.tishi);
        builder.setMessage(R.string.bunengcaoguo_chongxinxiadan);

        builder.addAction(new QMUIDialogAction(context, context.getString(R.string.confirm), new QMUIDialogAction.ActionListener() {
            @Override
            public void onClick(QMUIDialog qmuiDialog, int i) {
                qmuiDialog.dismiss();
            }
        }));

        QMUIDialog dialog = builder.create();
        dialog.setCancelable(false);
        dialog.show();
    }*/
}
